<template>
	<view>
		<appBar title="选择银行"></appBar>
		<scroll-view class="scrollList" scroll-y :scroll-into-view="scrollViewId">
			<block v-for="item in BankList" :key="item.letter">
				<view class="letter" :id="item.letter">{{item.letter}}</view>
				<view class="con">
					<view class="list" v-for="item in item.data" :key="item.value" @tap="selectBank(item)">
						<image :src="'/static/BankImg/'+item.value+'.jpg'" mode="aspectFill"></image>
						<text>{{item.text}}</text>
					</view>
				</view>
			</block>
		</scroll-view>
		<view class="uni-indexed-list-bar" :class="touchmove ? 'active' : ''" @tap.stop.prevent="letterSelect">
			<text v-for="(list, key) in BankList" :key="key" class="uni-indexed-list-text"
				:class="touchmoveIndex == key ? 'active' : ''">{{ list.letter }}</text>
		</view>
		<view class="uni-indexed-list-alert" v-if="touchmove">
			{{ BankList[touchmoveIndex].letter }}
		</view>

	</view>
</template>
<script>
	import bankList from '/static/json/bank.json'
	export default {
		data() {
			return {
				touchmove: false,
				touchmoveIndex: -1,
				itemHeight: 0,
				scrollViewId: '',
				BankList: bankList
			}
		},
		onLoad() {
		},
		onReady() {
			uni.getSystemInfo({
				success: res => {
					let info = uni.createSelectorQuery().select('.uni-indexed-list-bar')
					info
						.boundingClientRect(data => {
							let winHeight = data.height //元素在屏幕上的高度
							this.itemHeight = winHeight / this.BankList.length
						})
						.exec()
				},
			})
		},
		methods: {
			selectBank(data) {
				const jsonString = JSON.stringify(data);
				const obj = JSON.parse(jsonString);
				uni.navigateBack({
					delta: 1,
					success() {
						uni.$emit('bankBranch',obj.text);
					}
				})
			},
			letterSelect(e) {
				if (this.touchmove) return
				this.touchmove = true
				let pageY = e.target.offsetTop
				let index = Math.round(pageY / this.itemHeight)
				let item = this.BankList[index]
				if (item) {
					this.scrollViewId = item.letter
					this.touchmoveIndex = index
				}
				setTimeout(() => {
					this.touchmove = false
					this.touchmoveIndex = -1
				}, 500)
			},
		}
	}
</script>


<style lang="scss" scoped>
	.scrollList {
		background: #F6F9FC;
		height: 100vh;

		.letter {
			height: 64rpx;
			display: flex;
			align-items: center;
			padding: 0 30rpx;
			font-size: 24rpx;
			color: #222222;
		}

		.con {
			.list {
				background-color: #fff;
				display: flex;
				align-items: center;
				height: 122rpx;
				padding: 0 30rpx;
				border-bottom: 1px solid #F3F3F3;

				image {
					width: 48rpx;
					height: 48rpx;
				}

				text {
					color: #222222;
					font-size: 30rpx;
					margin-left: 30rpx;
				}
			}
		}
	}

	.uni-indexed-list-bar {
		display: flex;
		flex-direction: column;
		position: fixed;
		width: 40rpx;
		right: 14rpx;
		z-index: 10;
		top: 50%;
		transform: translateY(-50%);
	}


	.uni-indexed-list-text {
		color: #A3A2B1;
		font-size: 22rpx;
		text-align: center;
		height: 36rpx;
	}

	.uni-indexed-list-text.active,
	.uni-indexed-list-bar.active .uni-indexed-list-text.active {
		color: #222222;
		font-size: 26rpx;
	}

	.uni-indexed-list-alert {
		position: absolute;
		z-index: 20;
		width: 160rpx;
		height: 160rpx;
		left: 50%;
		top: 50%;
		margin-left: -80rpx;
		margin-top: -80rpx;
		border-radius: 80rpx;
		text-align: center;
		line-height: 160rpx;
		font-size: 70rpx;
		color: #fff;
		background-color: rgba(0, 0, 0, 0.5);
	}
</style>